package com.ct.application.adminlogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.adminlogs.model.po.SysLogSavePO;
import com.ct.application.adminlogs.model.po.SysLogSearchPO;
import com.ct.application.adminlogs.model.vo.SysLogVO;
import com.ct.application.adminlogs.service.ISysLogService;
import com.ct.repository.convert.CopyConvertUtils;
import com.ct.logs.orm.SysLog;
import com.ct.logs.responsity.ISysLogResponsity;
import com.ct.utils.threadlocal.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class SysLogService implements ISysLogService {

    @Resource
    private ISysLogResponsity sysLogResponsity;

    @Override
    public Page<SysLogVO> findSysLog(SysLogSearchPO sysLogSearchPO) {
        LambdaQueryWrapper<SysLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysLog::getSystemId, UserThreadLocal.get().getSystemId());
        if (StringUtils.isNotEmpty(sysLogSearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(SysLog::getUsername, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getMethod, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getAccount, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getProvince, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getCity, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getCountry, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getUserId, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getClassName, sysLogSearchPO.getKeyword())
                    .or().like(SysLog::getDescription, sysLogSearchPO.getKeyword()));
        }
        lambdaQueryWrapper.orderByDesc(SysLog::getCreateTime);
        // 查询返回
        Page<SysLog> page = new Page<>(sysLogSearchPO.getPageNo(), sysLogSearchPO.getPageSize());
        return sysLogResponsity.tranferPageBo(sysLogResponsity.page(page, lambdaQueryWrapper), SysLogVO.class);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateSysLog(SysLogSavePO sysLogSavePO) {
        SysLog sysLog1 = sysLogResponsity.countSystemIp(sysLogSavePO.getRequestIp(), sysLogSavePO.getMethod());
        if (sysLog1 == null) {
            SysLog sysLog = CopyConvertUtils.copyProperties(sysLogSavePO, SysLog.class);
            return sysLogResponsity.saveOrUpdate(sysLog);
        } else {
            sysLog1.setUpdateTime(new Date());
            return sysLogResponsity.updateById(sysLog1);
        }
    }

    @Override
    public boolean deleteSysLogById(Long id) {
        return sysLogResponsity.removeById(id);
    }

    @Override
    public SysLogVO getSysLogById(Long id) {
        return sysLogResponsity.tranferBo(sysLogResponsity.getById(id), SysLogVO.class);
    }
}
